package com.joinway.httpbaffle.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author kindey.s [kindey123@163.com]
 * @date 2020/6/8
 */
@RestController
public class VchCabinet {
    public static final Logger logger = LoggerFactory.getLogger(VchCabinet.class);

    @GetMapping(value = "hello")
    public String hello() {
        return "hello word";
    }

    /**
     * get接口入口
     *
     * @param info   接口内容
     *               gethumiture：温湿度查询
     *               getstates：密集架状态查询
     *               getposition：密集架每列位置状态查询
     * @param qu_num 区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 10:43
     */
    @RequestMapping(value = "/GDL", method = RequestMethod.GET)
    public String gdlGet(String info, String qu_num, String leftnum, String rightnum) {
        if (info.equals("gethumiture")) {
            return getHumiture(qu_num);
        } else if (info.equals("getstates")) {
            return getStates(qu_num);
        } else if (info.equals("getposition")) {
            return getPosition(Integer.parseInt(leftnum), Integer.parseInt(rightnum));
        } else if (info.equals("getposition")) {
            return getPosition(Integer.parseInt(leftnum), Integer.parseInt(rightnum));
        }
        return "不支持的接口类型";
    }

    /**
     * 温湿度查询
     *
     * @param qu_num 区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 10:43
     */
    private String getHumiture(String qu_num) {
        logger.info("qu_num:{}",qu_num);
        String result;
        if ("1".equals(qu_num)) {
            result = "{\"temperature\": \"26.5\", \"humidity\": \"0.66\", \"PM\": \"0.76\"}";
        } else {
            result = "{\"temperature\": \"22\", \"humidity\": \"0.66\", \"PM\": \"0.76\"}";
        }
        return result;
    }

    /**
     * 密集架状态查询
     *
     * @param qu_num 区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 10:43
     */
    private String getStates(String qu_num) {
        logger.info("qu_num:{}",qu_num);
        String result;
        if ("1".equals(qu_num)) {
            result = "{\"message\": \"禁止\"}";
        } else {
            result = "{\"message\": \"正常\"}";
        }
        return result;
    }

    /**
     * 位置查询
     * leftnum 左边区号，rightnum右边区号（固定列不为中的情况 固定列在左rightnum=0 固定列在右leftnum=0）,密集架最多显示24个,
     * 返回数组位置对应为当前架体状态 （0表示第一列）0没有到位1到位
     *
     * @param leftnum  固定列左边区号
     * @param rightnum 固定列右边区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 10:43
     */
    private String getPosition(int leftnum, int rightnum) {
        logger.info("leftnum:{}",leftnum);
        logger.info("rightnum:{}",rightnum);
        String result;
        if ("1".equals(leftnum)) {
            result = "{\n" +
                    "\"left\":{\n" +
                    "\"leftposition\": [0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\n" +
                    "\"rightposition\": [0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]\n" +
                    "},\n" +
                    "\"right\":{\n" +
                    "\"leftposition\": [0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\n" +
                    "\"rightposition\": [0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]\n" +
                    "}\n" +
                    "}\n";
        } else {
            result = "{\n" +
                    "\"left\":{\n" +
                    "\"leftposition\": [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\n" +
                    "\"rightposition\": [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]\n" +
                    "},\n" +
                    "\"right\":{\n" +
                    "\"leftposition\": [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\n" +
                    "\"rightposition\": [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]\n" +
                    "}\n" +
                    "}\n";
        }
        return result;
    }

    /**
     * post接口入口
     *
     * @param type      接口内容
     *                  Unlock：解锁
     *                  locking：锁定
     *                  stop：停止
     *                  leftmove：左移
     *                  rightmove：右移
     *                  merge：合架
     *                  aeration：通风
     *                  openshelf：自动开架
     *                  closeshelf：关闭自动开架
     *                  notice：公告
     *                  openframe：打开架体
     *                  openshelftask：开架任务
     * @param qu_num    区号
     * @param column    列
     * @param section   节
     * @param layer     层
     * @param direction 左/右
     * @param message   公告信息
     * @param name      用户名
     * @param data      Base64的特征数据
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    @RequestMapping(value = "/GDL", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public String gdlPost(
            @RequestParam(value = "type", required = false) String type
            , @RequestParam(value = "qu_num", required = false) String qu_num
            , @RequestParam(value = "column", required = false) String column
            , @RequestParam(value = "section", required = false) String section
            , @RequestParam(value = "layer", required = false) String layer
            , @RequestParam(value = "direction", required = false) String direction
            , @RequestParam(value = "message", required = false) String message
            , @RequestParam(value = "name", required = false) String name
            , @RequestParam(value = "data", required = false) String data
            , @RequestBody String body
            , HttpServletRequest request
    ) {
        logger.debug("Content-Type:{}",request.getContentType());
        logger.debug("body:{}",body);
        logger.info("type:{}",request.getParameter("type"));
        if (type.equals("Unlock")) {
            return unlock(qu_num);
        } else if (type.equals("locking")) {
            return locking(qu_num);
        } else if (type.equals("stop")) {
            return stop(qu_num);
        } else if (type.equals("leftmove")) {
            return leftmove(qu_num, column);
        } else if (type.equals("rightmove")) {
            return rightmove(qu_num, column);
        } else if (type.equals("merge")) {
            return merge(qu_num);
        } else if (type.equals("aeration")) {
            return aeration(qu_num);
        } else if (type.equals("openshelf")) {
            return openshelf(qu_num);
        } else if (type.equals("closeshelf")) {
            return closeshelf(qu_num);
        } else if (type.equals("notice")) {
            return notice(qu_num, message);
        } else if (type.equals("openframe")) {
            return openframe(qu_num, column, section, layer, direction);
        } else if (type.equals("openshelftask")) {
            return openshelftask();
        } else if (type.equals("registerFace")) {
            return registerFace(name);
        } else if (type.equals("recognition")) {
            return recognition(data);
        }
        return "不支持的接口类型";
    }

    /**
     * 解锁
     *
     * @param qu_num 区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String unlock(String qu_num) {
        logger.info("qu_num:{}",qu_num);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"解锁\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 锁定
     *
     * @param qu_num 区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String locking(String qu_num) {
        logger.info("qu_num:{}",qu_num);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"锁定\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 解锁
     *
     * @param qu_num 区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String stop(String qu_num) {
        logger.info("qu_num:{}",qu_num);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"停止\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 左移
     *
     * @param qu_num 区号
     * @param column 列
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String leftmove(String qu_num, String column) {
        logger.info("qu_num:{}",qu_num);
        logger.info("column:{}",column);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"左移\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 右移
     *
     * @param qu_num 区号
     * @param column 列
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String rightmove(String qu_num, String column) {
        logger.info("qu_num:{}",qu_num);
        logger.info("column:{}",column);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"右移\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 合架
     *
     * @param qu_num 区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String merge(String qu_num) {
        logger.info("qu_num:{}",qu_num);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"合架\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 通风
     *
     * @param qu_num 区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String aeration(String qu_num) {
        logger.info("qu_num:{}",qu_num);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"通风\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 打开自动开架
     *
     * @param qu_num 区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String openshelf(String qu_num) {
        logger.info("qu_num:{}",qu_num);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"打开自动开架\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 关闭自动开架
     *
     * @param qu_num 区号
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String closeshelf(String qu_num) {
        logger.info("qu_num:{}",qu_num);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"关闭自动开架\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 公告传递
     *
     * @param qu_num  区号
     * @param message 公告信息
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String notice(String qu_num, String message) {
        logger.info("qu_num:{}",qu_num);
        logger.info("message:{}",message);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"公告\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 打开架体
     *
     * @param qu_num    区号
     * @param column    列
     * @param section   节
     * @param layer     层
     * @param direction 左/右
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String openframe(String qu_num, String column, String section, String layer, String direction) {
        logger.info("qu_num:{}",qu_num);
        logger.info("column:{}",column);
        logger.info("section:{}",section);
        logger.info("layer:{}",layer);
        logger.info("direction:{}",direction);
        if ("1".equals(qu_num)) {
            return "{\"message\": \"打开架体\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 开架任务
     *
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String openshelftask() {
        logger.info("{}");
        return "{\"message\": \"开架任务\"}";
//        if ("1".equals(qu_num)) {
//            return "{\"message\": \"开架任务\"}";
//        } else {
//            return "{\"message\": \"500\"}";
//        }
    }

    /**
     * 人脸注册
     *
     * @param name 用户名
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String registerFace(String name) {
        logger.info("name:{}",name);
        if ("1".equals(name)) {
            return "{\"name\": \"1\",\"data\": \"image/jpg;base64,/9j/4AAQSkZJRgABAgEASABIAAD\"}";
        } else {
            return "{\"message\": \"500\"}";
        }
    }

    /**
     * 对比人脸
     *
     * @param data Base64的特征数据
     * @author Kindey.S [kindey123@163.com]
     * @date 2020/6/8 18:39
     */
    private String recognition(String data) {
        logger.info("data:{}",data);
        if ("image/jpg;base64,/9j/4AAQSkZJRgABAgEASABIAAD".equals(data)) {
            return "{\"msg\": \"1\"}";
        } else {
            return "{\"msg\": \"0\"}";
        }
    }
}
